package gui.visu.components;

import gui.visu.listeners.ListenerTravelInTimeBiSlider;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import tuio.components.BiSliderTouch;

import com.visutools.nav.bislider.BiSlider;
import com.visutools.nav.bislider.UnitPainterEvent;
import com.visutools.nav.bislider.UnitPainterListener;

import controls.DataControl;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class TimeTravelPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6269009578924401422L;

	protected static float LineX = 0f;

	private BiSliderTouch biSlider;

	private DataControl dataControl;

	public TimeTravelPanel(){
		this.biSlider = new BiSliderTouch(BiSlider.RGB);
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.add(Box.createRigidArea(new Dimension(20,0)));
		this.add(buildMoveAndZoomSliderPane());
		this.add(Box.createRigidArea(new Dimension(20,0)));
	}

	private JPanel buildMoveAndZoomSliderPane() {
		JPanel pane = new JPanel();

		try{
			biSlider.setToolTipText("Travel in time");
			biSlider.setUniformSegment(false);
			biSlider.setVisible(true);
			biSlider.setMinimumValue(0);
			biSlider.setMaximumValue(0);
			biSlider.setSegmentSize(30*60);
			biSlider.setMinimumColoredSize(400);
			biSlider.setMinimumColor(Color.BLUE);
			biSlider.setMaximumColor(Color.BLUE);
			biSlider.setColoredValues(0, 0);
			biSlider.setUnit("'");
			biSlider.setBackground(Color.GRAY);
			biSlider.setOpaque(true);
			biSlider.setSliderBackground(new Color(152, 152, 192));
			biSlider.setForeground(Color.BLACK);
			biSlider.setSound(false);
			biSlider.setArcSize(15);
			biSlider.setFont(new Font("SansSerif", Font.ITALIC|Font.BOLD, 9));
			biSlider.setPrecise(true); 
			biSlider.setMarginRulerTop(19);


			biSlider.addUnitPainterListener(new UnitPainterListener(){
				public void paintTick(UnitPainterEvent event){
					Graphics2D g2 = (Graphics2D)event.getGraphics();
					g2.setColor(biSlider.getForeground());
					Rectangle rect2 = event.getRectangle();
					FontMetrics fontMetrics0 = g2.getFontMetrics();

					if (event.getValue()%(30*60*60)==0){
						//g2.setColor(Color.BLUE);
						//g2.fillRect(rect2.x, rect2.y, rect2.width, rect2.height);
						g2.setColor(biSlider.getForeground());
						Font oldfont = g2.getFont();
						Font newfont = g2.getFont().deriveFont(Font.ITALIC, g2.getFont().getSize()+2);
						g2.setFont(newfont);
						int val = (int)(event.getValue()/(30*60*60));
						String text = ""+val+"h";
						g2.drawString(text, rect2.x+rect2.width/2-fontMetrics0.stringWidth(text)/2, rect2.y+rect2.height-5);
						g2.setFont(oldfont);
					} else  if (event.getValue()%(30*60*5)==0){
						//g2.setColor(Color.BLUE);
						//g2.fillRect(rect2.x, rect2.y, rect2.width, rect2.height);
						g2.setColor(biSlider.getForeground().darker());
						Font oldfont = g2.getFont();
						Font newfont = g2.getFont().deriveFont(Font.ITALIC, g2.getFont().getSize()+1);
						g2.setFont(newfont);
						int hrs = (int)(event.getValue()/(30*60*60));
						int val = (int)((event.getValue()-hrs*60*60*30)/(30*60));
						String text = ""+val+"'";
						g2.drawString(text, rect2.x+rect2.width/2-fontMetrics0.stringWidth(text)/2, rect2.y+rect2.height-1);
						g2.setFont(oldfont);
					} else{
						g2.setColor(biSlider.getForeground());
						//int val = (int)(event.getValue()/(30*60));
						//g2.setColor(Color.BLUE);
						//g2.fillRect(rect2.x, rect2.y, rect2.width, rect2.height);
						g2.setColor(biSlider.getForeground());

						int hrs = (int)(event.getValue()/(30*60*60));
						int min = (int)((event.getValue()-hrs*60*60*30)/(30*60));
						int sec = (int)((event.getValue()-hrs*60*60*30-min*30*60)/(30));
						String text = ""+hrs+"h";
						if (min<10)
							text+="0"+min+"'";
						else
							text+=min+"'";
						if (sec<10)
							text+="0"+sec+"\"";
						else
							text+=sec+"\"";

						g2.drawString(text, rect2.x+rect2.width/2-fontMetrics0.stringWidth(text)/2, rect2.y+rect2.height-1);
					}
				}
					public void paintValue(UnitPainterEvent event){
						Graphics2D g2 = (Graphics2D)event.getGraphics();
						g2.setColor(biSlider.getForeground());
						Rectangle rect2 = event.getRectangle();
						//int val = (int)(event.getValue()/(30*60));
						//g2.setColor(Color.BLUE);
						//g2.fillRect(rect2.x, rect2.y, rect2.width, rect2.height);
						g2.setColor(biSlider.getForeground());
						FontMetrics fontMetrics0 = g2.getFontMetrics();

						int hrs = (int)(event.getValue()/(30*60*60));
						int min = (int)((event.getValue()-hrs*60*60*30)/(30*60));
						int sec = (int)((event.getValue()-hrs*60*60*30-min*30*60)/(30));
						String text = ""+hrs+"h";
						if (min<10)
							text+="0"+min+"'";
						else
							text+=min+"'";
						if (sec<10)
							text+="0"+sec+"\"";
						else
							text+=sec+"\"";

						if (event.getValue()==biSlider.getMinimumColoredValue())
							g2.drawString(text, rect2.x+rect2.width-fontMetrics0.stringWidth(text), rect2.y+rect2.height-2);
						else
							g2.drawString(text, rect2.x, rect2.y+rect2.height-2);
					}
				}); 
			biSlider.setPrecise(true);
			biSlider.setArcSize(8);

			final JPopupMenu jPopupMenu7 = biSlider.createPopupMenu();
			biSlider.addMouseListener(new MouseAdapter(){
				@Override
				public void mousePressed(MouseEvent MouseEvent_Arg){
					if (MouseEvent_Arg.getButton()==MouseEvent.BUTTON3){
						jPopupMenu7.show(biSlider, MouseEvent_Arg.getX(), MouseEvent_Arg.getY());
					}
				}
			});

			Thread Thread1 = new Thread(){
				@Override 
				public void run() {
					while (true){
						while (LineX<1){
							LineX += 0.01f;
							try {sleep(20);} catch(InterruptedException  InterruptedException_Arg){}
							biSlider.repaint();
							yield();
						}
						while (LineX>0) {
							LineX -= 0.01f;
							try {sleep(20);} catch(InterruptedException  InterruptedException_Arg){}
							biSlider.repaint();
							yield();
						}
					}
				}
			};
			Thread1.setPriority(Thread.MIN_PRIORITY);
			Thread1.start();

			final JPopupMenu JPopupMenu2 = biSlider.createPopupMenu();
			biSlider.addMouseListener(new MouseAdapter(){
				@Override 
				public void mousePressed(MouseEvent MouseEvent_Arg){
					if (MouseEvent_Arg.getButton()==MouseEvent.BUTTON3){
						JPopupMenu2.show(biSlider, MouseEvent_Arg.getX(), MouseEvent_Arg.getY());
					}
				}
			});
			pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
			pane.add(biSlider);
			biSlider.setMinimumSize(new Dimension(100,30));
			}
			catch (Exception e) {

			} 
			return pane;
		}


		public BiSliderTouch getBiSlider() {
			return biSlider;
		}


		public void setBiSlider(BiSliderTouch biSlider) {
			this.biSlider = biSlider;
		}


		public void setRange(int tMin, int tMax) {
			biSlider.setMinimumValue(tMin);
			biSlider.setMaximumValue(tMax);
			int range = tMax-tMin;
			if(range>3600*30) biSlider.setSegmentSize(30*60*30);//1h->
			else if(range>1800*30) biSlider.setSegmentSize(10*60*30);//30min->1h
			else if(range>900*30) biSlider.setSegmentSize(5*60*30);//15 min -> 30 min
			else if(range>300*30) biSlider.setSegmentSize(2*60*30);//5 min -> 15 min
			else if(range>60*30) biSlider.setSegmentSize(30*30);//1 min -> 5 min
			else if(range>30*30) biSlider.setSegmentSize(15*30);//30 sec -> 1 min
			else biSlider.setSegmentSize(5*30);//0 sec -> 30 sec
			biSlider.setColoredValues(tMin, tMax);
		}


		public DataControl getDataControl() {
			return dataControl;
		}


		public void setDataControl(DataControl dataControl) {
			this.dataControl = dataControl;
			biSlider.addBiSliderListener(new ListenerTravelInTimeBiSlider(dataControl));
		}
	}
